define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
	$scope.rule_id="9999";
	$scope.rule_name="参照";
	
	$scope.find1=function(){
		httpService.post(app.urls.rule_select,$scope.form,function(data){
			if("0000" === data.code){
				$scope.rules = data.data;
				$scope.selectDictionary($scope.rules[0]);
			}else{
				alert(data.msg);
			}
		});
	}
	
	$scope.find=function(){
		
		httpService.postpage(app.urls.dictionary_select,{"fk_rule":$scope.rule_id},function(data){
			if("0000" === data.code){
				$scope.rows = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	
	
	var init=function(){
		$scope.find1();
		$scope.find();
	}
	init();
	
	//获取指定参照的字典
	$scope.selectDictionary=function(row){
		$scope.show="true";
		$scope.rule_id=row.id;
		$scope.rule_name=row.name;
		httpService.postpage(app.urls.dictionary_select,{"fk_rule":row.id},function(data){
			if("0000" === data.code){
				$scope.rows = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	
	//添加参照
	$scope.addRule=function(){
		eventBusService.publish("modal.page","modules/content/syssetting/dictionarysetting/addRule?FatherControllerName="+$scope.controllerName);
	}
	//编辑参照
	$scope.updateRule=function(){
		if("9999"===$scope.rule_id){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择您要修改的参照数据。"});
    	}else{
    		eventBusService.publish("modal.page","modules/content/syssetting/dictionarysetting/updateRule?FatherControllerName="+$scope.controllerName+"&id="+$scope.rule_id);
    	}
    }
	
	//添加字典
	$scope.addDictionary=function(){
		if("9999"===$scope.rule_id){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择您要添加字典的参照数据。"});
    	}else{
    		eventBusService.publish("modal.page","modules/content/syssetting/dictionarysetting/addDictionary?FatherControllerName="+$scope.controllerName+"&fk_rule="+$scope.rule_id);
    	}
	}
	
	//修改字典
	$scope.updateDictionary=function(){
		var values = [];
    	$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){ 
    		values.push($(this).val());
    	});
    	if(values.length < 1){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择您要修改数据。"});
    	}else if(values.length > 1){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"您选择了多行数据，请重新选择。"});
    	}else{
    		eventBusService.publish("modal.page","modules/content/syssetting/dictionarysetting/updateDictionary?id="+values[0]);
    	}
	}
	
	
	//按钮点击事件-删除字典
	$scope.deleteDictionary = function(){
		var values = [];
		var ids="";
    	$('#'+$scope.controllerName+' input[name="dataPk"]:checked').each(function(){ 
    		values.push($(this).val());
    		ids = ids+",'"+$(this).val()+"'";
    	});
    	
    	if(values.length < 1){
    		eventBusService.publish('modal.alert', {"title":"操作提示","content":"请选择您要删除数据。"});
    	}else{
    		eventBusService.publish("modal.alert",{"content":"你是否确认删除这"+values.length+"条数据？","isAutoClose":false,"confirm":function(){
    			httpService.post(app.urls.dictionary_delete,{"ids":ids.replace(',','')},function(data){
    				if("0000" === data.code){
    					$scope.find();
    	    		}else{
    	    			alert(data.msg);
    	    		}
    			});
    		},"cancel":function(){
    			//用户取消，不做操作
    		}});
    		
    		
    	}
	}
	
	//行选择checkbox
	$scope.trClick=function(row){
		$("input[value='"+row.id+"']").each(function(){
			if($(this).is(':checked')){
				$(this).prop("checked",false);
			}else{
				$(this).prop("checked",true);
			}
			
		});
	}
	
	//接收数据刷新
	eventBusService.subscribe('content.page.updateRule.reload.', function(event,data) {
		$scope.find1();
		$scope.find();
		$scope.rule_name=data.rule_name;
    });
	//接收数据刷新
	eventBusService.subscribe('content.page.reload.', function(event,data) {
		$scope.find1();
		$scope.find();
    });
		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAll = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
    
}}});